package com.atmaowei.service.impl;

import com.atmaowei.controller.request.BaseRequest;
import com.atmaowei.controller.request.CoursePageRequest;
import com.atmaowei.entity.Course;
import com.atmaowei.entity.Student;
import com.atmaowei.mapper.CourseMapper;
import com.atmaowei.service.CourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void save(Course course) {
        courseMapper.save(course);
    }

    @Override
    public String getCnoByCname(String cname) {
        return courseMapper.getCnoByCname(cname);
    }

    @Override
    public String getCnameByCno(String cno) {
        return courseMapper.getCnameByCno(cno);
    }

    @Override
    public PageInfo<Course> page(CoursePageRequest coursePageRequest) {
        PageHelper.startPage(coursePageRequest.getPageNum(), coursePageRequest.getPageSize());
        List<Course> list = courseMapper.page(coursePageRequest);
        for (Course course : list) {
            if (course.getCpno().equals("无")) {
                course.setCpname("无");
            } else {
                course.setCpname(getCnameByCno(course.getCpno()));
            }
        }
        PageInfo<Course> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void delete(String cno) {
        courseMapper.delete(cno);
        deleteCpnoEqualCno(cno);
    }

    @Override
    public void deleteCpnoEqualCno(String cpno) {
        courseMapper.deleteCpnoEqualCno(cpno);
    }

    @Override
    public void update(Course course, String oldCno) {
        courseMapper.update(course, oldCno);
    }

    @Override
    public Course getByCno(String cno) {
        Course course = courseMapper.getByCno(cno);
        if (course.getCpno().equals("无")) {
            course.setCpname("无");
        } else {
            course.setCpname(getCnameByCno(course.getCpno()));
        }
        return course;
    }

    @Override
    public List<Course> getAllCourse() {
        return courseMapper.getAllCourse();
    }

    @Override
    public void deleteSelected(List<Course> courses) {
        for (Course course : courses) {
            courseMapper.deleteSelected(course.getCno());
        }
    }
}
